package logic

import (
	"context"

	"go-weixin/app/usercenter/rpc/internal/domain"
	"go-weixin/app/usercenter/rpc/internal/svc"
	"go-weixin/app/usercenter/rpc/pb/proto/usercenter"

	"github.com/zeromicro/go-zero/core/logx"
)

type PowerLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
	powerDomain *domain.PowerDomain
}

func NewPowerLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PowerLogic {
	return &PowerLogic{
		ctx:         ctx,
		svcCtx:      svcCtx,
		Logger:      logx.WithContext(ctx),
		powerDomain: domain.NewPowerDomain(*svcCtx.ChenDb),
	}
}

func (l *PowerLogic) PowerAdd(in *usercenter.PowerAddReq) (*usercenter.PowerAddResp, error) {
	err := l.powerDomain.PowerAddExist(in.Name, in.Url)
	if err != nil {
		return nil, err
	}
	err = l.powerDomain.PowerAdd(in.Name, in.Method, in.Url, in.ParentId)
	if err != nil {
		return nil, err
	}
	return &usercenter.PowerAddResp{}, nil
}

func (l *PowerLogic) PowerUpdate(in *usercenter.PowerUpdateReq) (*usercenter.PowerUpdateResp, error) {
	err := l.powerDomain.PowerUpdateExist(in.Name, in.Id)
	if err != nil {
		return nil, err
	}

	err = l.powerDomain.PowerUpdate(in.Id, in.Method, in.Name, in.ParentId, in.Url)
	if err != nil {
		return nil, err
	}

	return &usercenter.PowerUpdateResp{}, nil
}

func (l *PowerLogic) PowerList(in *usercenter.PowerListReq) (*usercenter.PowerListResp, error) {
	powerList, err := l.powerDomain.PowerList(in.Page)
	if err != nil {
		return nil, err
	}
	var resp []*usercenter.PowerBase
	if len(powerList) > 0 {
		for _, power := range powerList {
			resp = append(resp, &usercenter.PowerBase{
				Id:       power.Id,
				Name:     power.Name,
				Method:   power.Method,
				ParentId: power.ParentId,
				Url:      power.Url,
				CreateAt: power.CreateAt.Unix(),
				UpdateAt: power.UpdateAt.Unix(),
			})
		}
	}
	return &usercenter.PowerListResp{
		PowerList: resp,
	}, nil
}

func (l *PowerLogic) PowerDetail(in *usercenter.PowerDetailReq) (*usercenter.PowerDetailResp, error) {
	power, err := l.powerDomain.PowerDetail(in.Id)
	if err != nil {
		return nil, err
	}

	return &usercenter.PowerDetailResp{
		PowerInfo: &usercenter.PowerBase{
			Id:       power.Id,
			Name:     power.Name,
			Method:   power.Method,
			ParentId: power.ParentId,
			Url:      power.Url,
			CreateAt: power.CreateAt.Unix(),
			UpdateAt: power.UpdateAt.Unix(),
		},
	}, nil
}

func (l *PowerLogic) PowerGetRole(in *usercenter.PowerGetRoleReq) (*usercenter.PowerGetRoleResp, error) {
	roleList, err := l.powerDomain.PowerGetRole(in.Id)
	if err != nil {
		return nil, err
	}
	var resp []*usercenter.RoleBase
	if len(roleList) > 0 {
		for _, role := range roleList {
			resp = append(resp, &usercenter.RoleBase{
				Id:       role.Id,
				Name:     role.Name,
				ParentId: role.ParentId,
				Weight:   role.Weight,
				CreateAt: role.CreateAt.Unix(),
				UpdateAt: role.UpdateAt.Unix(),
			})
		}
	}
	return &usercenter.PowerGetRoleResp{
		RoleList: resp,
	}, nil
}

func (l *PowerLogic) PowerGetUser(in *usercenter.PowerGetUserReq) (*usercenter.PowerGetUserResp, error) {
	userList, err := l.powerDomain.PowerGetUser(in.Id)
	if err != nil {
		return nil, err
	}
	var resp []*usercenter.UserBase
	if len(userList) > 0 {
		for _, user := range userList {
			resp = append(resp, &usercenter.UserBase{
				Id:       user.Id,
				Activity: user.Activity,
				Gender:   user.Gender,
				Country:  user.Country,
				Email:    user.Email,
				OpenId:   user.OpenId,
				Phone:    user.Phone,
				Name:     user.Name,
				CreateAt: user.CreateAt.Unix(),
				UpdateAt: user.UpdateAt.Unix(),
			})
		}
	}
	return &usercenter.PowerGetUserResp{
		UserList: resp,
	}, nil
}
